package cn.itcast.bos.action.take_delivery;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.utils.BaseAction;

public class ImageAction extends BaseAction<Object> {
	private File imgFile;
	private String imgFileFileName;

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	@Action(value = "image_upload", results = { @Result(name = "success", type = "json") })
	public String uploadImg() throws Exception {
		System.out.println("接收图片的名称:" + imgFileFileName);
		// System.out.println("接收文件的类型:"+imgFileContentType);

		// 图片保存的磁盘绝对路径
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload");
		// 图片保存显示的项目相对路径
		String urlPath = ServletActionContext.getRequest().getContextPath() + "/upload/";

		// 获取文件的后缀名
		String ext = imgFileFileName.substring(imgFileFileName.lastIndexOf("."));
		// 保存的文件名
		String filename = UUID.randomUUID() + ext;
		
		// 保存图片
		File file = new File(savePath + "/" + filename);
		System.out.println(file.getAbsolutePath());
		if(imgFile!=null){
			FileUtils.copyFile(imgFile, file);
		}

		// 通知浏览器上传成功
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("error", 0);
		map.put("url", urlPath + filename);
		ActionContext.getContext().getValueStack().push(map);

		return SUCCESS;
	}

	@Action(value = "image_manage", results = { @Result(name = "success", type = "json") })
	public String manage() {
		// 根目录路径，可以指定绝对路径
		String rootPath = ServletActionContext.getServletContext().getRealPath("/upload/") ;
		//根目录URL，可以指定绝对路径
		String rootUrl  = ServletActionContext.getRequest().getContextPath() + "/upload/";
		//当前上传目录
		File currentPathFile = new File(rootPath);
		// 图片扩展名
		String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" };
		// 遍历目录的文件信息
		List<Map<String,Object>> fileList = new ArrayList<Map<String,Object>>();
		
		if (currentPathFile.listFiles() != null) {
			for (File file : currentPathFile.listFiles()) {
				Map<String, Object> hash = new HashMap<String, Object>();
				String fileName = file.getName();
				if (file.isDirectory()) {
					hash.put("is_dir", true);
					hash.put("has_file", (file.listFiles() != null));
					hash.put("filesize", 0L);
					hash.put("is_photo", false);
					hash.put("filetype", "");
				} else {
					String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
					hash.put("is_dir", false);
					hash.put("has_file", false);
					hash.put("filesize", file.length());
					hash.put("is_photo", Arrays.<String> asList(fileTypes).contains(fileExt));
					hash.put("filetype", fileExt);
				}
				hash.put("filename", fileName);
				hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
				fileList.add(hash);
			}
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("moveup_dir_path", null);
		map.put("current_dir_path",rootPath );
		map.put("current_url", rootUrl);
		map.put("total_count", fileList.size());
		map.put("file_list", fileList);
		
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}
}
